package org.xilin

class BookController {
    
    def authenticateService
    def semesterService

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list")
    }

    def list = {
        //params.max = Math.min(params.max ? params.int('max') : 10, 100)
        def bookList = semesterService.getListBySemester(Book)
        render(template: "list", model: [instanceList: bookList, bookInstanceTotal: bookList.count()])
    }

    def create = {
        def bookInstance = new Book()
        bookInstance.properties = params
        render(template: "create", model: [bookInstance: bookInstance])
    }

    def save = {
        log.debug("save")
        def bookInstance = null

        if (params.id) {
            bookInstance = Book.get(params.id)
            bookInstance.properties = params
        }
        else {
            bookInstance = new Book(params)
        }

        if (!bookInstance.validate()) {
            flash.message = "Missing required data"
            render(template: params.view, model: [bookInstance: bookInstance])
            return
        }

        if (!bookInstance.save(flush: true)) {
            flash.message = "faile to save data"
            render(template: params.view, model: [bookInstance: bookInstance])
            return
        }
        //render(template: params.view, model: [bookInstance: bookInstance])
        redirect(action: "list")
    }

    def show = {
        def bookInstance = Book.get(params.id)
        if (!bookInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])}"
            redirect(action: "list")
        }
        else {
            render(template: "show", model: [bookInstance: bookInstance])
        }
    }

    def edit = {
        def bookInstance = Book.get(params.id)
        if (!bookInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])}"
            redirect(action: "list")
        }
        else {
            render(template: "edit", model: [bookInstance: bookInstance])
        }
    }

    def update = {
        def bookInstance = Book.get(params.id)
        if (bookInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (bookInstance.version > version) {
                    
                    bookInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'book.label', default: 'Book')] as Object[], "Another user has updated this Book while you were editing")
                    render(view: "edit", model: [bookInstance: bookInstance])
                    return
                }
            }
            bookInstance.properties = params
            if (!bookInstance.hasErrors() && bookInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'book.label', default: 'Book'), bookInstance.id])}"
                redirect(action: "show", id: bookInstance.id)
            }
            else {
                render(view: "edit", model: [bookInstance: bookInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def bookInstance = Book.get(params.id)
        if (bookInstance) {
            Course course = Course.findByBook(bookInstance)
            if (course) {
                flash.message = "Cannot delet book '" + bookInstance.name + "' it is referenced by course " + course.name + "."
            }
            else {
                try {
                    bookInstance.delete(flush: true)
                    flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'book.label', default: 'Book'), params.id])}"
                }
                catch (org.springframework.dao.DataIntegrityViolationException e) {
                    flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'book.label', default: 'Book'), params.id])}"
                }
                catch(org.hibernate.exception.ConstraintViolationException e) {
                    flash.message = "Book '${bookInstance.title}' could not be deleted"
                }
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])}"
        }
        redirect(action: "list")
    }
}
